#include <bits/stdc++.h>
using namespace std;
const int N = 4e6 + 7;

int buc[N];

int n;

template <class T = int>
inline T read() {
    char c = getchar();
    bool sign = (c == '-');
    
    while (c < '0' || c > '9')
        c = getchar(), sign |= (c == '-');
    
    T x = 0;
    
    while ('0' <= c && c <= '9')
        x = (x << 1) + (x << 3) + (c & 15), c = getchar();
    
    return sign ? (~x + 1) : x;
}

signed main() {
    freopen("contest.in", "r", stdin);
    freopen("contest.out", "w", stdout);
    scanf("%d", &n);
    vector<pair<int, int> > vec;
    
    for (int i = 1; i <= n; ++i) {
        int k = read();
        
        while (k--)
            vec.emplace_back(read(), i);
    }
    
    sort(vec.begin(), vec.end());
    int ans = 2e9, cnt = 0;
    
    for (int i = 0, j = 0; i < vec.size(); ++i) {
        cnt += !buc[vec[i].second], ++buc[vec[i].second];
        
        while (j < i && buc[vec[j].second] > 1)
            --buc[vec[j].second], ++j;
        
        if (cnt == n)
            ans = min(ans, vec[i].first - vec[j].first);
    }
    
    printf("%d\n", ans);
    return 0;
}


